package com.zmn.plat.utils.callback;

import com.zmn.plat.manager.dictionary.ChannelDict;
import com.zmn.plat.model.entity.channel.Channel;
import com.zmn.plat.services.interfaces.channel.ChannelService;
import org.slf4j.Logger;

import java.util.Objects;
import java.util.function.Predicate;

/**
 * @author tanbiao
 * @description:渠道二级不存在数据，返回一级渠道数据
 * @modifier
 * @since 2020/11/11 14:59
 **/
public interface Callback<T, M extends ChannelService> {
    default T doCallback(Integer channelId, M m, Predicate predicate, Logger log) {
        T t = redo(channelId);
        if (predicate.test(t)) {

            if (Objects.nonNull(log)) {
                log.info("渠道id查询数据为空，检查是否为二级渠道..channelId:{}", channelId);
            }

            Channel channel = m.findByKey(channelId);
            if (Objects.nonNull(channel) && Objects.equals(channel.getRank(), ChannelDict.RANK_TWO)) {

                Integer parentChannelId = channel.getParentId();
                if (Objects.nonNull(log)) {
                    log.info("渠道id查询数据为空,返回父级渠道数据.channelId:{}，parentId:{}", channelId, parentChannelId);
                }

                if (Objects.nonNull(parentChannelId)) {
                    t = redo(parentChannelId);
                }
            }
        }
        return t;
    }

    T redo(Integer channelId);
}
